package com.dayuanit.dy15.spider.task;

import com.dayuanit.dy15.spider.util.ApiConnector;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.HashSet;
import java.util.Set;

public class DaoHangTask {

    //一级导航栏地址集合
    private static final Set<String> thirdNavUrl = new HashSet();

    public static void main(String[] args) {
        String html = ApiConnector.get("http://e.dangdang.com/list-XS2-dd_sale-0-1.html");

        Document document = Jsoup.parse(html);
        Elements dlElements = document.select("div.first_level.publication.publisher");
        dlElements.forEach(element -> {
            String text = element.select("a").first().text();
            System.out.println(">" + text);
            Elements lis = element.select("ul.second_level").select("li");
            lis.forEach(e2 -> {
                if (e2.select("a").first() != null) {
                    String title = e2.select("a").first().text();
                    String url = e2.select("a").first().attr("href");
                    System.out.println(">>>" + title + " " + url);
                    Elements el = e2.select("ul.third_level").select("a");
                    el.forEach(e3 -> {
                        String text3 = e3.text();
                        String url3 = e3.attr("href");
                        System.out.println(">>>>>>" + text3 + " " + url3);
                        thirdNavUrl.add(url3);
                    });
                }
            });
        });


        processBook();
    }


    public static void processBook() {
        int count = 0;
        for (String url : thirdNavUrl) {
            String html = ApiConnector.get(url);
            Document document = Jsoup.parse(html);

//            System.out.println(html);
            Elements aElements = document.select("div.book.book_list.clearfix").select("a");
            aElements.forEach(element -> {
                Element bookElement = element.select("div.bookinfo").first();
                String title = bookElement.select("div.title").first().text();
                String author = bookElement.select("div.author").first().text();
                String price = bookElement.select("div.price").first().text();
                String imgUrl = element.select("span.bookcover").select("img").last().attr("src");

                System.out.println(title + " " + author + " " + price + " " + imgUrl);
            });

            if (++count > 2) {
                break;
            }

            System.out.println("-------------" + count);
        }
    }
}
